package com.he.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Department;
import com.he.pojo.Permission;
import com.he.service.permission.PermissionServiceI;

@Controller
@RequestMapping("permission")
public class PermissionController {

	@Autowired
	PermissionServiceI ps;
	@RequestMapping("/showPermission")
	public String permissionShow(HttpServletRequest request,Permission permission) {
		List<Permission> rList= ps.getPermissions(null, permission);
		request.setAttribute("list", rList);
		/*为department总页数赋值,向前端传总页数*/
		PageInfo<Permission> info=new PageInfo<>(rList);
		//为permission总页数赋值
		permission.setPages(info.getPages());
		//为permission总记录数赋值
		permission.setTotal(info.getTotal());
		/*为permission返回的controller链接赋值*/
		permission.setUrl("showPermission.do");
		/*向前端传参数保存在user中*/
		request.setAttribute("permission", permission);
		return "permissionmanage";
	}
	@RequestMapping("/editPermission")
	public String permissionEdit(HttpServletRequest request, Permission permission) {
		int r=ps.update(permission);
		if (r>0) {
			return "redirect:showPermission.do";
		}else {
			return "";
		}
		
	}
	@RequestMapping("/addPermission")
	public String permissionAdd(HttpServletRequest request, Permission permission) {
		int r=ps.insert(permission);
		if (r>0) {
			return "redirect:showPermission.do";
		}else {
			return "defeat";
		}
		
	}
	@RequestMapping("/delPermission")
	@ResponseBody
	public String permissionDel(int id  ) {
		int r=ps.delete(id);
		if (r>0) {
			return "success";
		}else {
			return "defeat";
		}
		
	}
}
